package com.chixing.dao.impl;

import com.chixing.dao.DepartmentDao;
import com.chixing.entity.Department;

import java.util.List;

public class DepartImpl implements DepartmentDao {

    JDBCTemplate<Department> jdbcTemplate=new JDBCTemplate<>(new DepartmentRowMapper());

    @Override
    public int insert(Department department) {
        String sql="insert into dept values(?,?,?)";
        Object[] args={department.getDeptNo(),department.getDeptName(),department.getDeptLoc()};
        return jdbcTemplate.update(sql,args);
    }

    @Override
    public int update(Department department) {
        String sql="update dept set dname=?,loc=? where deptno=?";
        Object[] args={department.getDeptName(),department.getDeptLoc(),department.getDeptNo()};
        return jdbcTemplate.update(sql,args);
    }

    @Override
    public int delete(int deptNo) {
        String sql="delete from dept where deptno=?";
        Object[] args={deptNo};
        return jdbcTemplate.update(sql,args);
    }

    @Override
    public Department selectByNo(int deptNo) {
        String sql="select * from dept where deptno=?";
        Object[] args={deptNo};
        return jdbcTemplate.selectForObject(sql, args);
    }

    @Override
    public List<Department> selectAll() {
        String sql="select * from dept";
        return jdbcTemplate.selectForList(sql, null);
    }
}
